/*
 * Powered By Rubyliu
 * QQ:876181
 * Since 2012 - 2012
 */

package com.henme.crm.web.admin;

import com.henme.crm.entity.admin.Authority;
import com.henme.crm.service.admin.AuthorityService;
import com.henme.crm.util.dwz.AjaxDone;
import com.henme.crm.util.page.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;



/**
 * @author Ruby liu
 * @version 1.0
 * @since 1.0
 */
@Controller
@RequestMapping("/authority")
public class AuthorityController   {
	
	@Autowired
	private AuthorityService authorityService;
	
	/**
	 * page 获得从页面传过来的分页信息
	 * @param page
	 * @param model
	 * @return
	 */
	@RequestMapping("/list")
	public String list(@ModelAttribute("page") Page<Authority> page,
			@ModelAttribute("authority") Authority authority,
			@RequestParam(value="navtabid",required = false) String navtabid,
			Model model) {
		page.setT(authority);
		page=authorityService.getAuthorityList(page);
		model.addAttribute("page", page);
		model.addAttribute("authority", authority);
		model.addAttribute("navtabid", navtabid);
		return "authority/list";
	}
	
	/**
	 * 编辑页面
	 * @param model
	 * @param id
	 * @param navtabid
	 * @return
	 */
	@RequestMapping(value="/edit/{id}")
	public String prepare(Model model,@PathVariable("id") Long id,
			@RequestParam("navtabid") String navtabid
		) {
		model.addAttribute("navtabid", navtabid);
		model.addAttribute("authority", authorityService.selectAuthority(id));
		return "authority/edit";
	}
	
	/**
	 * 保存更新
	 * @param authority
	 * @param navtabid
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/update")
	public AjaxDone update(@ModelAttribute("authority")Authority authority,
			@RequestParam("navtabid") String navtabid
			) 
	{
		authorityService.updateAuthority(authority);
		AjaxDone a=new AjaxDone();
		a.setStatusCode("200");
		a.setCallbackType("closeCurrent");
		a.setMessage("更新成功");
		a.setNavTabId(navtabid); //重新加载这个nav
		return a;
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete/{id}")
	public AjaxDone delete(@PathVariable("id") Long id) {
		authorityService.deleteAuthority(id);
		AjaxDone a=new AjaxDone();
		a.setStatusCode("200");
		a.setMessage("删除成功");
		return a;
	}
	
	/**
	 * 新增页面
	 * @param model
	 * @param navtabid
	 * @return
	 */
	@RequestMapping("/add")
	public String add(Model model,@RequestParam("navtabid") String navtabid) {
		model.addAttribute("navtabid", navtabid);
		return "authority/edit";
	}

	/**
	 * 保存新增
	 * @param authority
	 * @param navtabid
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/insert")
	public AjaxDone insert(@ModelAttribute("authority")Authority authority,
			@RequestParam("navtabid") String navtabid) {
		authorityService.insertAuthority(authority);
		AjaxDone a=new AjaxDone();
		a.setStatusCode("200");
		a.setCallbackType("closeCurrent");
		a.setMessage("新增成功");
		a.setForwardUrl(""); 
		a.setNavTabId(navtabid); //重新加载这个nav
		return a;
	}

}

